package com.eqdd.yiqidian.ui.discover.makerspace

import android.app.Activity
import android.content.Intent
import android.support.v7.widget.RecyclerView
import android.view.View
import com.eqdd.yiqidian.R
import com.eqdd.yiqidian.base.Config
import com.eqdd.yiqidian.bean.IndustriesBean
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditEnterBean
import com.eqdd.yiqidian.kotlin.bottomDialog
import com.eqdd.yiqidian.ui.base.EditTextActivity
import com.eqdd.yiqidian.ui.base.SlimBaseMultiEditActivity
import kotlinx.android.synthetic.main.activity_recyclerview.*
import org.jetbrains.anko.startActivityForResult
import org.jetbrains.anko.toast

/**
 * Created by jhj on 18-8-4.
 */
class IndustryWayActivity : SlimBaseMultiEditActivity() {
    var type: Int = -1
    private var hourPrice: String? = null
    private var dayPrice: String? = null

    override fun initBinding() {
        clearData()
        setContentView(R.layout.activity_recyclerview)
        initTopTitleBar(View.VISIBLE, "服务方式")
        val t = intent.getIntExtra(Config.TYPE, 0)
        val bean = intent.getSerializableExtra("data") as IndustriesBean.ModeBean?
        initTopRightText("确定") {
            if (checkParams()) { //1-新增 0-修改
                val str = if (t == 1) {
                    """{
                        "type":"$type",
                        "OneDayPrice":"$dayPrice",
                        "OneHourPrice":"$hourPrice"
                    }"""
                } else {
                    """{
                        "modeId":${bean?.id},
                        "type":$type,
                        "oneDayPrice":"$dayPrice",
                        "oneHourPrice":"$hourPrice"
                    }"""
                }
                val intent = Intent()
                intent.putExtra("data", str)
                setResult(Activity.RESULT_OK, intent)
                finish()
            } else {
                toast(msg)
            }
        }
    }

    override fun initRecyclerView(): RecyclerView {
        return recyclerView
    }

    override fun itemClick(p: Int) {
        when (p) {
            0 -> {
                bottomDialog(listOf("线下服务", "电话服务", "线上服务")) {
                    when (it) {
                        1 -> {
                            type = 0
                            slimAdapterEx.getDataItem(0).content = "线下服务"
                            slimAdapterEx.notifyItemChanged(0)
                        }
                        2 -> {
                            type = 1
                            slimAdapterEx.getDataItem(0).content = "电话服务"
                            slimAdapterEx.notifyItemChanged(0)
                        }
                        3 -> {
                            type = 2
                            slimAdapterEx.getDataItem(0).content = "线上服务"
                            slimAdapterEx.notifyItemChanged(0)
                        }
                    }
                }
            }
            1 -> startActivityForResult<EditTextActivity>(1, "title" to "小时/元",
                    "tip" to "小时/元", "content" to hourPrice, "onlyNumber" to true)
            2 -> startActivityForResult<EditTextActivity>(2, "title" to "天/元",
                    "tip" to "天/元", "content" to dayPrice, "onlyNumber" to true)

        }
    }

    override fun setEditBeans() {
        val bean = intent.getSerializableExtra("data") as IndustriesBean.ModeBean?
        val type = when {
            bean?.type == 0 -> "线下服务"
            bean?.type == 1 -> "电话服务"
            bean?.type == 2 -> "线上服务"
            else -> ""
        }
        hourPrice = bean?.oneHourPrice
        dayPrice = bean?.oneDayPrice

        datas.add(SlimEditEnterBean("服务方式", type, 0, "请选择").setNeed(true).setDeal(true))
        datas.add(SlimEditEnterBean("小时/元", bean?.oneHourPrice, 1, "请选择").setNeed(true).setDeal(true))
        datas.add(SlimEditEnterBean("天/元", bean?.oneDayPrice, 2, "请选择").setNeed(true).setDeal(true))
    }


    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == Config.INTENT_CONTENT) {
            val str = data?.getStringExtra("content")
            when (requestCode) {
                1 -> {
                    hourPrice = str
                    slimAdapterEx?.getDataItem(1)?.content = str
                    slimAdapterEx?.notifyItemChanged(1)
                }
                2 -> {
                    dayPrice = str
                    slimAdapterEx?.getDataItem(2)?.content = str
                    slimAdapterEx?.notifyItemChanged(2)
                }
            }
        }
    }
}